import 'dart:io';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter_ahj_app/base/basewidget/HttpUtils/ApiService.dart';
import 'package:flutter_ahj_app/base/basewidget/HttpUtils/HttpBaseConst.dart';
import 'package:flutter_ahj_app/base/basewidget/HttpUtils/HttpKCBaseConst.dart';
import 'package:flutter_ahj_app/base/basewidget/HttpUtils/HttpOpenBaseConst.dart';
import 'package:flutter_ahj_app/base/basewidget/HttpUtils/HttpPHPBaseConst.dart';
import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';
import 'package:flutter_ahj_app/base/bean/Result.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
class HttpClient {
  static HttpClient instance;
  static ApiService apiService;
  HttpClient(String type){
    var options = new BaseOptions(
      connectTimeout: 50000,
      receiveTimeout: 30000,
      baseUrl: type == 'ahj' ? HttpBaseConst.serverUrl : HttpKCBaseConst.serverUrl,// api 头部例如 https://www.baidu.com
    );
    Dio dio = new Dio(options);
    dio.interceptors.add(AuthInterceptor()); // 添加 token
    dio.interceptors
        .add(LogInterceptor(responseBody: true, requestBody: true)); //添加日志
    dio.interceptors
        .add(InterceptorsWrapper(onRequest: (RequestOptions options,RequestInterceptorHandler requestInterceptorHandler) async {
          options.headers["app_identifier"] = "azxs";
      options.headers["Authorization"] =
          "Bearer " + UserInfoUtils.getUserToken();
      options.headers['device'] = Platform.isIOS ? 'iPhone' : 'Android';
      options.headers["Content-type"] = "application/json";
      options.headers["Access-Token"] = "Bearer " + UserInfoUtils.getUserToken();
      options.headers["User-Agent"] = Uri.encodeComponent( "app( " + UserInfoUtils.getPackageVeName()+");user("+UserInfoUtils.getUserId()+");"+UserInfoUtils.getDeVeName());
      print(UserInfoUtils.getUserToken());
      options.headers["tenantId"] = "ahj";
      options.headers["Origin"] = "ahjApp";
      return requestInterceptorHandler.next(options) ; //continue

    }, onResponse: (Response response,ss) {
      Result result = Result.fromJson(json.decode(response.toString()));
      if (result.code == 1) {
        String msg =
        (result.msg == null || result.msg == "") ? "获取数据时发生错误" : result.msg;
        ToastUtils.showToast(msg);
      }
      return ss.next(response); // continue
    }, onError: (DioError e,dd) {
      return dd.next(e); //continue
    }));
    apiService = new ApiService(dio);
  }

  static ApiService getInstance({bool debug}) {
    if (instance == null) {
      instance = new HttpClient('ahj');
    }
    //change
    apiService.dio.options.baseUrl = debug == null ? HttpBaseConst.serverUrl : 'http://127.0.0.1:4523/m1/1181463-0-default/';
    return _getApiService();
  }
  static ApiService getPHPInstance({bool debug}) {
    if (instance == null) {
      instance = new HttpClient('ahj');
    }
    apiService.dio.options.baseUrl = debug == null ? HttpPHPBaseConst.serverUrl : 'http://127.0.0.1:4523/m1/1181463-0-default/';
    return _getApiService();
  }
  static ApiService getOpenInstance(String type) {
    if (instance == null) {
      instance = new HttpClient('ahj');
    }
    switch (type) {
      case 'zixun':
        apiService.dio.options.baseUrl = HttpOpenBaseConst.serverZixunUrl;
        break;
      default:
    }
    return _getApiService();
  }
  static ApiService getKCInstance() {
    if (instance == null) {
      instance = new HttpClient('wx');
    }
    apiService.dio.options.baseUrl = HttpKCBaseConst.serverUrl;
    return _getApiService();
  }
  static  ApiService _getApiService() {
    return apiService;
  }
}

class AuthInterceptor extends Interceptor {
  String PLATFORM = "android"; //可根据代码进行判断
  String CLIENT = "store";
  @override
  onRequest(RequestOptions options,RequestInterceptorHandler requestInterceptorHandler) async {
    Map<String, String> headers = new Map();
    options.headers = headers;
    return super.onRequest(options,requestInterceptorHandler);
  }
}
